package com.pointwest.pls.core;

import java.sql.SQLException;
import java.util.ArrayList;

import com.pointwest.pls.constant.Constant;
import com.pointwest.pls.dao.SeatDAO;
import com.pointwest.pls.domainMapper.*;
import com.pointwest.pls.domain.Employee;
import com.pointwest.pls.domain.QuadrantSeat;
import com.pointwest.pls.domain.Seat;
import com.pointwest.pls.domain.SeatType;
import com.pointwest.pls.domain.Location;


public class SeatProcessor {
	
	public Seat getSeat(int employeeId) throws ClassNotFoundException, SQLException, Exception {
		DomainMapper domMapper = new DomainMapper();
		
		Seat employeeSeat = new Seat();
		
		employeeSeat = domMapper.getSeat(employeeId);
		employeeSeat.getEmployee().setProjectList(domMapper.getProject(employeeId));
		return employeeSeat;
	}
	
	public ArrayList<QuadrantSeat> getQuadrantMap(String quadrant) throws ClassNotFoundException, SQLException, Exception {
		DomainMapper domMapper = new DomainMapper();
		ArrayList<QuadrantSeat> quadrantSeats = new ArrayList<QuadrantSeat>();
		String locationID = "3";
		
		quadrantSeats = domMapper.getQuadrantSeats(quadrant, locationID);
		
		return quadrantSeats;
	}
	
	public int updateSeat(int employeeId, String seatCode) throws ClassNotFoundException, SQLException, Exception {
		SeatDAO seatDAO = new SeatDAO();
		int rowsChanged = 0;
		rowsChanged = seatDAO.updateSeat(employeeId, seatCode);
		return rowsChanged;
	}
	
	public int deleteSeat(int employeeId, String seatCode) throws ClassNotFoundException, SQLException, Exception {
		SeatDAO seatDAO = new SeatDAO();
		int rowsChanged = 0;
		rowsChanged = seatDAO.deleteSeat(employeeId, seatCode);
		return rowsChanged;
	}
	
	public int insertSeat(int employeeId, String seatCode) throws ClassNotFoundException, SQLException, Exception {
		SeatDAO seatDAO = new SeatDAO();
		int rowsChanged = 0;
		String locationDetailCode = seatCode.substring(0, 5);
		
	
		String locationDetailID;
		String quadrant;
		String seatTypeID = "1";
		String row;
		String col;
		if (locationDetailCode.equalsIgnoreCase("PTC12")){
			quadrant = seatCode.substring(6, 7);
			row = seatCode.substring(9, 10);
			col = seatCode.substring(7, 8);
			locationDetailID="3";
		}else {
			row = seatCode.substring(8, 9);
			col = seatCode.substring(6, 7);
			quadrant = seatCode.substring(5, 6);
		
			if(locationDetailCode.equalsIgnoreCase("PIC3F")){
				
				locationDetailID="1";			
			}else{
				
				locationDetailID="2";
			}
		}
	
		
		rowsChanged = seatDAO.insertSeat(employeeId, seatCode, quadrant, locationDetailID, seatTypeID, col, row);
		return rowsChanged;
	}
	
	public boolean checkSeat(String seatCode) throws SQLException, Exception{
		SeatDAO seatDAO = new SeatDAO();
		boolean isAvail = seatDAO.checkSeatAvailability(seatCode);
		
		return isAvail;
	}
	

}
